#pragma once

#include "CmDBThreadInterface.h"
#include "pay_proxy_session.h"

class CPayProxyServerSink
{
public:
	virtual void OnKillSelf(RtResult aRt) = 0;
	virtual ~CPayProxyServerSink() {}
};

class CPayProxyServer
	: public CRtTimerWrapperSink
	, public IConnProviderSink
	, public IBaseSessionSink
{
public:
	CPayProxyServer();
	virtual ~CPayProxyServer();
	
public:
	static CPayProxyServer* Instance();
	RtResult Initialize(const TransportAddress& cmdport, CPayProxyServerSink* psink);

	void Release(void);


protected: // from CRtTimerWrapperSink
	virtual void OnTimer(CRtTimerWrapper* aID);

	// from IConnProviderSink
	virtual void OnConnectionReady(RtResult aResult,
		IConnection* pConn,
		const CRtInetAddr* pAddr);

	virtual void OnKillSelf(RtResult aRt);

public:
	virtual void OnDisconnect(RtResult aReason, CBaseSession* pSession);

public:
	RtResult PostEvent(IDBEvent* pDBEvent, WORD wThreadNo);

private:
	RtResult AddConnect(IConnection* pConn, const TransportAddress& localaddr);

	RtResult DbInitialize();
	RtResult PgInitialize();
	RtResult McInitialize();

	DWORD	CreateSessionID();

private:
	CRtTimerWrapper		m_TimerDbHeartBeat;
	CPayProxyServerSink*	m_pSink;
	IConnProvider*		m_pConnProvider;

	TransportAddress	m_cmdPayAddr;

	CRtAutoPtr<CDBThreadManager> m_pDBTreadMgr;
};






